package com.xxp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxp.controller.request.TravelRequest;
import com.xxp.entity.Book;
import com.xxp.entity.Message;
import com.xxp.entity.Travel;
import com.xxp.entity.Tuijian;
import com.xxp.mapper.TravelMapper;
import com.xxp.service.TravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements TravelService {
    @Autowired
    private TravelMapper travelMapper;

    public List<Travel> selectAll() {
        List<Travel> travels = travelMapper.selectAll();
        return travels;
    }

    /**
     * 分页查询
     * @param travelRequest 分页展示实体类
     * @return
     */
    @Override
    public Object listByCondition(TravelRequest travelRequest) {
        PageHelper.startPage(travelRequest.getPageNum(), travelRequest.getPageSize());
        List<Travel> travels = travelMapper.listByCondition(travelRequest);
        return new PageInfo<>(travels);
    }

    @Override
    public Travel selectById(Integer id) {
        Travel travel = travelMapper.selectById(id);
        return travel;
    }

    @Override
    public List<Message> findMes(Integer id) {
        List<Message> mes = travelMapper.findMes(id);
        return mes;
    }

    @Override
    public void pushMsg(Message message) {
        Date d = new Date();
        message.setCreatetime(d);
        travelMapper.pushMsg(message);
    }

    @Override
    public void updateRecommend( Tuijian tuijian) {
        //根据当前浏览景点ID查询tuijian表里是否有数据
        List<Tuijian> determine = travelMapper.determine(tuijian.getTravelId());
        //如果查询出来的为空则插入并修改人数,否则抛异常
        if (determine.isEmpty()) {
            //往tuijian表里面插入
            travelMapper.insertdetermine(tuijian);
            Travel travel = travelMapper.selectById(tuijian.getTravelId());
            Integer num = travel.getNum();
            num++;
            travel.setNum(num);
            //更新lvyou_zhinan表
            travelMapper.updateRecommend(travel);
        } else {
            throw new RuntimeException("您已经推荐过了");
        }

    }

    @Override
    public void deleteMsg(Integer id) {
        travelMapper.deleteMsg(id);
    }

    @Override
    public List<Message> findAllMes() {
        //查询所有消息
        List<Message> allMes = travelMapper.findAllMes();
        //循环遍历消息
        for (Message allMe : allMes) {
            //得到消息的parentId
            String parentId = allMe.getParentId();
            //根据parentId查询景点名称
            Travel travel = travelMapper.TravelName(Integer.valueOf(parentId));
            //景点名称替换掉parentI
            allMe.setParentId(travel.getName());

        }
        return allMes;
    }

    @Override
    public List<Message> findAllMesByUser(Integer id) {
        List<Message> allMesByUser = travelMapper.findAllMesByUser(id);
        for (Message message : allMesByUser) {
            String parentId = message.getParentId();
            Travel travel = travelMapper.TravelName(Integer.valueOf(parentId));
            message.setParentId(travel.getName());
        }
        return allMesByUser;
    }
    @Override
    public void insertTravel(Travel travel) {
        travel.setCreatetime(new Date());
        travelMapper.instertTravel(travel);
    }

    @Override
    public Object backTravel(TravelRequest travelRequest) {
        PageHelper.startPage(travelRequest.getPageNum(),travelRequest.getPageSize());
        List<Travel> travels = travelMapper.backSelect(travelRequest);
        return new PageInfo<>(travels);

    }

    @Override
    public void deltetTravel(Integer id) {
        travelMapper.deleteTravel(id);
    }

    @Override
    public void updateTravel(Travel travel) {
        travelMapper.updateTravel(travel);
    }

    @Override
    public Travel TravelName(Integer id) {
        return travelMapper.TravelName(id);
    }




}
